package net.stogi.unicore.beyond.api.model;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Michał Stogowski
 */
public class InputDefinition extends Entity {

	private static final long serialVersionUID = 4447263258512913845L;

	private Integer index;

	private InputGroup inputGroup;

	private InputType inputType;

	private String name;
	
	private Map<String, PropertyValue> properties;

	public InputDefinition() {
		properties = new HashMap<String, PropertyValue>();
	}

	public InputDefinition(String name, InputType inputType) {
		this();
		assert name != null;
		assert inputType != null;
		this.name = name;
		this.inputType = inputType;
		// Create properties based on data in input type.
		for (PropertyType propertyType : inputType.getProperties().values()) {
			PropertyValue propertyValue = new PropertyValue(propertyType);
			properties.put(propertyType.getKey(), propertyValue);
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!(obj instanceof InputDefinition)) {
			return false;
		}
		InputDefinition other = (InputDefinition) obj;
		// Check name.
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		}
		else if (!name.equals(other.name)) {
			return false;
		}
		return true;
	}

	public Integer getIndex() {
		return index;
	}

	public InputGroup getInputGroup() {
		return inputGroup;
	}

	public InputType getInputType() {
		return inputType;
	}

	public String getName() {
		return name;
	}

	public Map<String, PropertyValue> getProperties() {
		return properties;
	}

	public PropertyValue getProperty(String key) {
		return properties.get(key);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	public PropertyValue selectPropertyWithId(Long id) {
		assert id != null;
		PropertyValue selectedProperty = null;
		for (PropertyValue property : properties.values()) {
			if (id.equals(property.getId())) {
				selectedProperty = property;
			}
		}
		return selectedProperty;
	}

	public void setIndex(Integer index) {
		this.index = index;
	}

	public void setInputGroup(InputGroup inputGroup) {
		this.inputGroup = inputGroup;
	}

	public void setInputType(InputType inputType) {
		this.inputType = inputType;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setProperties(Map<String, PropertyValue> properties) {
		this.properties = properties;
	}
}
